<?php

define('DP_PRI', BP . 'pri' . DS);
define('DP_CODE', DP_PRI . 'code' . DS);
define('DP_CONFIG', DP_PRI . 'etc' . DS);
define('DP_DESIGN', DP_PRI . 'design' . DS);
define('DP_LOCALIZE', DP_PRI . 'localize' . DS);
define('DP_PUB', BP . 'pub' . DS);
define('DP_TMP', BP . 'tmp' . DS);
define('FP_AREA_CONFIG', 'area.yml');
define('FP_APP_CONFIG', 'local.yml'); //for local
//define('FP_APP_CONFIG', 'live.yml');//for live

class App {

//    private static $_areaSetting = NULL;
    private static $_appConfig = NULL;
    private static $_domainConfig = NULL;
	private static $_application = NULL;
	
    public static function getAppConfig() {
        return self::$_appConfig;
    }

    public static function getDomainConfig() {
        return self::$_domainConfig;
    }

    public static function getModel($name) {
        return Kd_Loader::loadInstance(self::getModelClass($name));
    }

    public static function getHelper($name) {
        if (strpos($name, '/') === FALSE) {
            $name .= '/helper';
        }
        return Kd_Loader::loadSingleton('helper_' . str_replace('/', '_', $name));
    }

    public static function getModelClass($name) {
        if (strpos($name, '/')) {
            return 'model_' . str_replace('/', '_', $name);
        }
        return $name;
    }

    public static function getModule($name,$domain){
        if (strpos($name, '/')) {
            $name = str_replace('/', '_module_', $name);
        }
        $class = Kd_Loader::loadClass($name);
        return new $class($domain);
    }

    public static function getBlock($name) {
        $namespace = App::getDomainConfig()->getSetting('namespace');
        return Kd_Loader::loadInstance($namespace . '_module_' . $name . '_render');
    }

    public static function run() {
        self::$_application = new Dcmp_Web_Application(Kd_Util_Yaml::parseFile(DP_CONFIG . FP_APP_CONFIG));
        self::$_application->run();
//        Kd_Loader::addScope(self::$_webConfig->getSetting('versions'));
//
//        Kd_Html_Config::setYesOption('是', '1');
//        Kd_Html_Config::setNoOption('否', '0');
//
//        $request = Kd_Web::getRequest();
//        $serverName = $request->getServerName();
//        $url = $request->getUri();
//        if (substr($url, -1) != '/') {
//            $url .= '/';
//        }
//
//        if(substr($url, 0, strlen('/backend/')) == '/backend/'){
//            self::$_domainConfig =  new Kd_Configuration(Kd_Util_Yaml::parseFile(DP_CONFIG . 'area' . DS . 'backend.yml'));
//        }else{
//            self::$_domainConfig = self::matchWebsiteArea($serverName, $url);
//        }
//        Kd_Loader::loadSingleton('application_' . self::$_domainConfig->getSetting('namespace'))->run();
    }

    private static function matchWebsiteArea($serverName, $url) {
        $matchUrl = substr($url, 0, strpos($url, '/', 1) + 1);
        $files = Kd_Util_IO::getFiles(DP_CONFIG . 'area', 'yml');
        foreach ($files as $file) {
            if($file == 'backend'){
                continue;
            }
            $areaSetting = Kd_Util_Yaml::parseFile($file['filename']);
            $areaDomain = $areaSetting['domain_name'];
            $areaUrl = $areaSetting['url'];
            if (!empty($areaDomain)) {
                if ($areaDomain[0] == '@') {
                    if (!preg_match('/' . substr($areaDomain, 1) . '/i', $serverName)) {
                        continue;
                    }
                } elseif ($areaDomain != $serverName) {
                    continue;
                }
            }

            if ($areaUrl[0] == '@') {
                if (preg_match('/' . substr($areaUrl, 1) . '/i', $url)) {
                    return $areaSetting;
                }
            } elseif (substr($url, 0, strlen($matchUrl)) == $areaUrl) {
                return new Kd_Configuration($areaSetting);
            }
        }
        $areaSetting = Kd_Util_Yaml::parseFile(DP_CONFIG . 'area' . DS . self::$_webConfig->getSetting('default_area') . '.yml');
        return new Kd_Configuration($areaSetting);
    }

}